package cn.bb.controller;

import cn.bb.model.Pic;
import cn.bb.service.IPicService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class PicController {
    @Resource
    private IPicService picService;
    @RequestMapping("/addPic")
    public ModelAndView addPic(HttpServletRequest request, Integer id, @Validated Pic pics,
                               BindingResult bindingResult, MultipartFile pic){

        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for(ObjectError objectError :allErrors){
                System.out.println(objectError.getDefaultMessage());
            }
            return new ModelAndView("picError");
        }
        //原始名称
        String originalFilename = pic.getOriginalFilename();
        System.out.println("上传图片的名字为："+originalFilename);
        return null;
    }
    public IPicService getPicService() {
        return picService;
    }

    public void setPicService(IPicService picService) {
        this.picService = picService;
    }
}
